<?php

namespace app\api\model\xykeep\user;

use think\Model;
use traits\model\SoftDelete;
use app\api\model\xykeep\User; 
use app\api\model\xykeep\Store as StoreModel;
use app\api\model\xykeep\Coach as CoachModel;
use app\api\model\xykeep\Goods as GoodsModel;
use app\api\model\xykeep\Course as CourseModel;

class Favorite extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'xykeep_user_favorite';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    ];
    

    /**
     * 添加｜删除
     */
    public static function add($params)
    {
        extract($params);
        $user = User::info();
        
        $favorite = self::get(['target_id' => $target_id, 'user_id' => $user->id]);
        if ($favorite) {
            $favorite->delete();
            $favorite = null;
        }else{
           $favorite = self::create([
                'user_id' => $user->id,
                'target_id' => $target_id,
            ]);
        }
        return $favorite;
    }

    /**
     * params 请求参数
     */
    public static function getList($params)
    {
        extract($params);
        $user = User::info();
        $list = self::where(['favior.user_id'=>$user->id,'course.status'=>'normal','course.deletetime'=>null])
            ->field('favior.id as favior_id,favior.target_id,course.image,max_apply,course.name,course.tags,course.price,course.id')
            ->alias('favior')->join('xykeep_course course','favior.target_id=course.id')->order('id desc')->paginate();


        return $list;
    }


}
